using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Services.Interface;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text.Json;

namespace RndMaterialDisposalService.Pages.EmailTemplates.WorkshopProcessing
{
    /// <summary>
    /// 
    /// </summary>
    [IgnoreAntiforgeryToken]
    public class IndexModel : PageModel
    {

        private readonly ISMOService service;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_service"></param>
        public IndexModel(ISMOService _service)
        {
            service = _service;
        }

        /// <summary>
        /// 
        /// </summary>
        [BindProperty]
        public WorkshopProcessinFormData FormData { get; set; } = default!;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formdata"></param>
        /// <returns></returns>
        public void OnPost([FromBody] WorkshopProcessinFormData formdata)
        {
            FormData = formdata;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formid"></param>
        /// <returns></returns>
        public async Task<IActionResult> OnGetAsync(int? formid)
        {
            if (formid == null)
            {
                return NotFound();
            }
            var data = await service.GetApplicationDataByFormId(formid ?? 0);
            if (string.IsNullOrEmpty(data.FormData))
            {
                return NotFound();
            }
            var jsonSerializerOptions = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
            FormData = JsonSerializer.Deserialize<WorkshopProcessinFormData>(data.FormData!, jsonSerializerOptions);
            return Page();
        }
    }

}
